package com.cn.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cn.model.system.SysUser;
import com.cn.model.vo.RouterVo;
import com.cn.model.vo.SysUserQueryVo;
import com.cn.system.mapper.SysUserMapper;
import com.cn.system.service.SysMenuService;
import com.cn.system.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysMenuService sysMenuService;

    @Override
    public IPage<SysUser> selectPage(Page<SysUser> pageParam, SysUserQueryVo userQueryVo) {
        return sysUserMapper.selectPage(pageParam, userQueryVo);
    }

    /**
     * 更新状态
     * @param id id
     * @param status status
     */
    @Override
    public void updateStatus(Long id, Integer status) {
        SysUser user = sysUserMapper.selectById(id);
        user.setStatus(status);
        sysUserMapper.updateById(user);
    }

    //根据username查询数据
    @Override
    public SysUser getByUsername(String username) {
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        return sysUserMapper.selectOne(wrapper);
    }


    //根据用户名获取用户信息（基本信息 菜单权限 按钮权限数据）
    @Override
    public Map<String, Object> getUserInfo(String username) {
        HashMap<String, Object> result = new HashMap<>();
        //根据username查询用户基本信息
        SysUser sysUser = this.getByUsername(username);
        //根据userid获取菜单权限值
        List<RouterVo> routerVolist=sysMenuService.getUserMenuList(sysUser.getId());
        //根据userid获取用户按钮权限
        List<String> permsList=sysMenuService.getUserButtonList(sysUser.getId());

        result.put("name",username);
        result.put("avatar","https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        result.put("roles","[\"admin\"]");
        //菜单权限数据
        result.put("routers",routerVolist);
        //按钮权限数据
        result.put("buttons",permsList);

        return result;
    }
}
